//
//  XNetworkManager+PigeonExtension.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/15.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import Foundation

// MARK: - 微鸽各种请求数据基类
fileprivate extension XNetworkManager{
    /// 点赞基类请求
    ///
    /// - Parameters:
    ///   - url: <#url description#>
    ///   - params: <#params description#>
    ///   - complete: <#complete description#>
    func basePigeonLikeRequest(url:String,params:[String:Any],complete:@escaping (_ list:String?,_ isSuccess:Bool)->()){
        request(urlString: url, params: params) { (json, isSuccess) in
            if isSuccess{
                guard  let dic = (json as? [String:Any]),//转成字典
                    let str = dic["like_list"] as? String,//取出reslut对应的字典数组
                    let _ = Int(dic["error_code"] as? String ?? "1")//取出页码
                    else{
                        //以上步骤出现错误，直接返回
                        complete(nil,false)
                        return
                }
                //得到数组，返回
                complete(str,true)
            }else{
                //请求失败，返回
                complete(nil,false)
            }
        }
    }
    
    
    /// 获取鸽友圈列表
    ///
    /// - Parameters:
    ///   - url: <#url description#>
    ///   - params: <#params description#>
    ///   - complete: <#complete description#>
    func basePigeonListRequest(url:String,params:[String:Any],complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        request(urlString: url, params: params) { (json, isSuccess) in
            if isSuccess{
                
                guard  let dic = (json as? [String:Any]),//转成字典
                    let array:[[String:Any]] = dic["result"]  as? [[String:Any]],//取出reslut对应的字典数组
                    let cpage:Int = Int(dic["cpage"] as? String ?? "1")//取出页码
                    else{
                        //以上步骤出现错误，直接返回
                        complete(1,nil,false)
                        return
                }
                //得到数组，返回
                complete(cpage,array,true)
            }else{
                //请求失败，返回
                complete(1,nil,false)
            }
        }
    }
    func getSingleSquareRequest(url:String,params:[String:Any],complete:@escaping (_ json:[String:Any]?,_ isSuccess:Bool)->()){
        request(urlString: url, params: params) { (json, isSuccess) in
            if isSuccess{
                guard  let dic = (json as? [String:Any])//转成字典
                    else{
                        //以上步骤出现错误，直接返回
                        complete(nil,false)
                        return
                }
                //得到数组，返回
                complete(dic,true)
            }else{
                //请求失败，返回
                complete(nil,false)
            }
        }
        
    }
   
    
}
extension XNetworkManager{
    /// 登录用的，获取验证码：目前无法短信登录，只能发短信过来
    ///
    /// - Parameters:
    ///   - url: <#url description#>
    ///   - params: <#params description#>
    ///   - complete: <#complete description#>
    func getVerificationCode(phone:String,complete:@escaping (_ code:String?,_ isSuccess:Bool)->()){
        let url = "http://m.chinaxinge.com/androidapk/admin/send_telyzm.asp"
        let params = ["Tel":phone]
        request(urlString: url, params: params) { (json, isSuccess) in
            if isSuccess{
                guard  let dic = (json as? [String:Any]),//转成字典
                    let code:String = dic["yzm"] as? String
                    else{
                        //以上步骤出现错误，直接返回
                        complete(nil,false)
                        return
                }
                //得到数组，返回
                complete(code,true)
            }else{
                //请求失败，返回
                complete(nil,false)
            }
        }
    }
    /// 点赞接口
    ///
    /// - Parameters:
    ///   - id: 鸽友圈ID
    ///   - complete: 完成回调,_ list:[[String:Any]]?,_ isSuccess:Bool)->()
    ///
    func likeSquare(id:Int,complete:@escaping (_ list:String?,_ isSuccess:Bool)->()){
        let url = "http://news.chinaxinge.com/gyq/circle/circle_like.asp"
        //FIXME:点赞接口，这里的us_id暂时时写死
        let params = ["newsid":id,"us_id":718900]
        
        basePigeonLikeRequest(url: url, params: params, complete: complete)
    }
}

// MARK: - 鸽友圈相关
extension XNetworkManager{
    
    //    http://news.chinaxinge.com/gyq/circle/circle_news_v1.asp?classfy=0&cpage=1
    /// 获取鸽友圈列表信息
    ///
    ///
    /// - Parameters:
    ///   - page: 页码 默认为1
    ///   - complete:  page: 返回当前数据是第几页，供加载下一页的时候作参考,_ list:[[String:Any]]?,_ isSuccess:Bool
    func getSquareList(page:Int,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        let url = "http://news.chinaxinge.com/gyq/circle/circle_news_v1.asp"
        let params = ["classfy":0,"cpage":page]
        
        basePigeonListRequest(url: url, params: params, complete: complete)
    }
    func getSingleSquare(id:Int,complete:@escaping (_ json:[String:Any]?,_ isSuccess:Bool)->()){
        let url = "http://news.chinaxinge.com/gyq/circle/circle_news_detail_v1.asp"
        let params = ["newsid":id]
        getSingleSquareRequest(url: url, params: params, complete: complete)
    }
    
}

// MARK: - 鸽闻相关
extension XNetworkManager{
    
    
    
    /// /// 获取园地新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getGardenNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 0,page:page, complete: complete)
    }
    /// /// 获取公棚新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getBobNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 1,page:page, complete: complete)
    }
    /// /// 获取协会新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getAssociationNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 2,page:page, complete: complete)
    }
    /// /// 获取俱乐部新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getClubNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 3,page:page, complete: complete)
    }
    
    /// /// 获取博客新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getBlogNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 4,page:page, complete: complete)
    }
    /// /// 获取百科新闻
    ///
    ///
    /// - Parameters:
    ///   - page: 页码:从1开始，默认为1
    /// - Parameter complete: list:字典[[String:Any]]?,_ isSuccess:Bool
    func getEncyclopediasNews(page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        getNews(id: 4,page:page, complete: complete)
    }
    /// 获取鸽闻
    ///
    /// - Parameters:
    ///   - id: 0：园地 1：公棚 2：协会 3：俱乐部 4：博客 5：百科
    ///- page: 页码:从1开始，默认为1
    ///   - complete:   page: Int 当前数据是第几页
    ///                 list:[[String:Any]]?,
    ///                 isSuccess:Bool
    private func getNews(id:Int,page:Int=1,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        let url = "http://m.chinaxinge.com/androidapk/getnews_v1.asp"
        let params = ["id":id,"cpage":page]
        basePigeonListRequest(url: url, params: params, complete: complete)
        
    }
    
}

